"""
线程安全的单例装饰器
"""
from functools import wraps

from threading import RLock

def singleton(cls):

    instances = {}
    locker = RLock()
    @wraps(cls)
    def wrapper(*args, **kwargs):
        if cls not in instances:
            with locker:
                if cls not in instances:
                    instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return wrapper